#include "StdAfx.h"
#include "GraphicControlMedia.h"
#include "MediaObject.h"
#include "MultiMediaDlg.h"

#include <string>

IMPLEMENT_DYNAMIC(GraphicControlMedia,CWnd)

GraphicControlMedia::GraphicControlMedia(void)
{
}

GraphicControlMedia::~GraphicControlMedia(void)
{
}
BEGIN_MESSAGE_MAP(GraphicControlMedia, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BTN_FULL_SCREEN,OnBnClickedFullScreen)
	ON_BN_CLICKED(IDC_BTN_PLAY_PAUSE,OnBnClickedPlayPause)
	ON_BN_CLICKED(IDC_BTN_STOP,OnBnClickedStop)
	ON_BN_CLICKED(IDC_BTN_NEXT,OnBnClickedNext)
	ON_BN_CLICKED(IDC_BTN_PREVIOUS,OnBnClickedPrevious)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_SEEK_BAR, OnNMReleasedcaptureSldSeekBar)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_VOLUME, OnNMReleasedcaptureSldVolume)
	ON_WM_ERASEBKGND()
	ON_WM_TIMER()
END_MESSAGE_MAP()

int GraphicControlMedia::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	m_SliderSeeking.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ,CRect(0,0,900,30),this,IDC_SLD_SEEK_BAR);
	m_SliderSeeking.ShowWindow(SW_SHOW);
	m_stcTimePlaying.Create(_T("00:00:00"),WS_CHILD | WS_VISIBLE,CRect(0,0,80,30),this,NULL);
	m_stcTimePlaying.ShowWindow(SW_SHOW);
	m_stcTab.Create(_T("/"),WS_CHILD | WS_VISIBLE,CRect(0,0,80,30),this,NULL);
	m_stcTab.ShowWindow(SW_SHOW);
	m_stcTimeTotal.Create(_T("00:00:00"),WS_CHILD | WS_VISIBLE,CRect(0,0,80,30),this,NULL);
	m_stcTimeTotal.ShowWindow(SW_SHOW);
	m_btncheckLoop.Create(_T("Loop"),WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,CRect(0,0,60,30),this,NULL);
	m_btncheckLoop.ShowWindow(SW_SHOW);
	m_btnPlayPause.Create(_T("Play"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PLAY_PAUSE);
	m_btnPlayPause.ShowWindow(SW_SHOW);
	m_btnStop.Create(_T("Stop"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_STOP);
	m_btnStop.ShowWindow(SW_SHOW);
	m_btnNext.Create(_T("Next"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_NEXT);
	m_btnNext.ShowWindow(SW_SHOW);
	m_btnPrevious.Create(_T("Previous"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PREVIOUS);
	m_btnPrevious.ShowWindow(SW_SHOW);
	m_btnFullScreen.Create(_T("Full Screen"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_FULL_SCREEN);
	m_btnFullScreen.ShowWindow(SW_SHOW);
	m_btncheckmute.Create(_T("Mute"),WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,CRect(0,0,60,30),this,NULL);
	m_btncheckmute.ShowWindow(SW_SHOW);
	m_SliderVolume.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ,CRect(0,0,0,30),this,NULL);
    m_SliderVolume.ShowWindow(SW_SHOW);
	
	m_bIsPlaying = false;
	m_nVol = -1;
	m_SliderSeeking.SetRange(MIN_RANGE_SEEK_SLIDER, MAX_RANGE_SEEK_SLIDER);
	m_SliderVolume.SetRange(MIN_RANGE_VOL_SLIDER,MAX_RANGE_VOL_SLIDER);
	return 0;
}

void GraphicControlMedia::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
    CWnd *parent = GetParent();
	CRect rect;
	this->GetClientRect(&rect);
	parent->GetClientRect(&rect);
	CRect rtViewArea(0,rect.bottom-rect.top-150,rect.right, rect.bottom);//here
   

	int nMarginLeft = (rtViewArea.right - rtViewArea.left )/3;
	int nMarginTop  = (rtViewArea.bottom - rtViewArea.top );
	
	m_SliderSeeking.SetWindowPos(parent,0,nMarginTop-150, rtViewArea.right, 30, SWP_NOZORDER);
	m_stcTimePlaying.SetWindowPos(parent,nMarginLeft/8,nMarginTop-115, 70, 30, SWP_NOZORDER);
	m_stcTab.SetWindowPos(parent,nMarginLeft/8+70,nMarginTop-115, 5, 30, SWP_NOZORDER);
	m_stcTimeTotal.SetWindowPos(parent,nMarginLeft/8+80,nMarginTop-115, 70, 30, SWP_NOZORDER);
	m_btncheckLoop.SetWindowPos(parent,4*nMarginLeft/8+30,nMarginTop-120, 80, 30, SWP_NOZORDER);
	m_btnPlayPause.SetWindowPos(parent,nMarginLeft-nMarginLeft/8, nMarginTop-120, 100, 30, SWP_NOZORDER);
	m_btnStop.SetWindowPos(parent,nMarginLeft-nMarginLeft/8+110, nMarginTop-120, 100, 30, SWP_NOZORDER);
    m_btnNext.SetWindowPos(parent,nMarginLeft-nMarginLeft/8+220, nMarginTop-120, 100, 30, SWP_NOZORDER);
	m_btnPrevious.SetWindowPos(parent, nMarginLeft-nMarginLeft/8+330, nMarginTop-120, 100, 30, SWP_NOZORDER);
	m_btnFullScreen.SetWindowPos(parent, nMarginLeft-nMarginLeft/8+440, nMarginTop-120, 100, 30, SWP_NOZORDER);
	m_btncheckmute.SetWindowPos(parent, nMarginLeft-nMarginLeft/8+590, nMarginTop-120, 70, 30, SWP_NOZORDER);
	m_SliderVolume.SetWindowPos(parent, nMarginLeft-nMarginLeft/8+660, nMarginTop-120,4*nMarginLeft/8, 30, SWP_NOZORDER);

}

void GraphicControlMedia::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
}

BOOL GraphicControlMedia::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//CRect rect;

	//GetClientRect(&rect);
	//CBrush myBrush(COLOR_VIEW_AREA);    // dialog background color
	//CBrush *pOld = pDC->SelectObject(&myBrush);
	//BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
	//pDC->SelectObject(pOld);    // restore old brush
	//return bRes;   

	return CWnd::OnEraseBkgnd(pDC);
}
void GraphicControlMedia::OnBnClickedFullScreen()
{
	/*MessageBox(_T("MFC TEAM 14"),MediaObject., 
      MB_ICONERROR | MB_OK);*/

//TRACE(_T("")+MediaObjects);
	// GraphicShowFullVideo m_gShowFullVideo ;
	//m_gShowFullVideo.OnShowWindow();
	//m_gShowFullVideo.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	//m_gShowFullVideo.ShowWindow(SW_SHOW);
}
CString               GraphicControlMedia::m_strFileName;
CString               GraphicControlMedia::m_strFilePath;

void GraphicControlMedia::OnBnClickedPlayPause()
{
 	CMultiMediaDlg multiDlg;
	m_VideoMng.SetFilePath(m_strFilePath);
	m_VideoMng.MultiMedia_Play();
	m_strTotalTime = m_VideoMng.GetTimeDuration();
	m_stcTimeTotal.SetWindowTextW((LPCTSTR)m_strTotalTime);
	KillTimer(MY_TIMEREVENT);
	SetTimer(MY_TIMEREVENT,1000,(TIMERPROC)NULL);
	m_bIsPlaying = true;
}

void GraphicControlMedia::OnBnClickedStop()
{
	m_VideoMng.MultiMedia_Stop();
}

void GraphicControlMedia::OnBnClickedNext()
{
}

void GraphicControlMedia::OnBnClickedPrevious()
{
}

void GraphicControlMedia::OnNMReleasedcaptureSldSeekBar(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	if(m_SliderSeeking.GetPos() == MAX_RANGE_SEEK_SLIDER)
	{
	}
}

void GraphicControlMedia::OnNMReleasedcaptureSldVolume(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	m_nVol = m_SliderVolume.GetPos();
	MediaVolume(m_nVol);
}

void GraphicControlMedia::MediaVolume(int nVol)
{
	long lVol = 0;
	if(nVol > -8)
	{
		int val = 4000 / 8;
		lVol = nVol * val;
	}
	else
	{
		int val = 1000;
		lVol = nVol * val;
	}
	m_VideoMng.SetVolume(lVol);
}
void GraphicControlMedia::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
	case MY_TIMEREVENT:
		DoTimeStuff();
		break;
	}
	CWnd::OnTimer(nIDEvent);
}

int GraphicControlMedia::DoTimeStuff()
{
	if(!m_bIsPlaying || m_VideoMng.m_lTimeElapsed >= m_VideoMng.m_lTotalDuration)
		return 0;
	//m_stcTimePlay.SetWindowTextW(m_VideoMng.GetTimeElapsedOfMedia());
	m_SliderSeeking.SetPos(m_VideoMng.m_nPos);
	if(m_VideoMng.m_nPos == MAX_RANGE_SEEK_SLIDER)
	{
		//MediaStop();
	}
	else
		//m_btnPlay.EnableWindow(false);
	return 1;
}
